//
//  RigthViewController.m
//  Tour
//
//  Created by 张张烨 on 16/3/14.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "RigthViewController.h"
#import "MYShipinshouchang;.h"
#import "AppDelegate.h"
#import "MyhuanfuViewController.h"

@interface RigthViewController () <UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) UIView *backView;

@end

@implementation RigthViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self tableView1];

    
    [self rongyunConnect];
    /**<  夜间没事 */
//    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//    self.backView  = [[UIView alloc] initWithFrame:FRAME];
//    
//    UIImageView *img= [[UIImageView alloc]initWithFrame:FRAME];
//    img.image =[UIImage imageNamed:@"狗狗"];
//    [self.backView addSubview:img];
//    
////    self.backView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"单身狗"]];
//    self.backView.alpha = 0.5;
//    self.backView.userInteractionEnabled = NO;
//    [delegate.window addSubview:_backView];
//    _backView.hidden = YES;

}

- (void)tableView1
{
    self.tableView = [[BaseTableView alloc] initWithFrame:self.view.frame style:(UITableViewStyleGrouped)];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消分割线
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }if(section == 1){
        return 1;
    } else{
        return 2;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[BaseTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    
    if (indexPath.section == 0 && indexPath.row ==0) {
        
    } else if (indexPath.section == 0 && indexPath.row == 2){
        cell.textLabel.text = @"咨询客服";
        
    }
    
    if (indexPath.section == 1 && indexPath.row ==0) {
        cell.textLabel.text = @"一件换肤";
         UISwitch *seitch = [[UISwitch alloc]init];
        cell.accessoryView = seitch;
        
        [seitch addTarget:self action:@selector(swich:) forControlEvents:UIControlEventValueChanged];
    }
    if (indexPath.section == 2 && indexPath.row ==1) {
        cell.textLabel.text = @"换皮肤";
    }
    
    return cell;
}

/**<  按钮方法中心 */
-(void)swich:(UISwitch *)button
{
    
    if (button.on) {
        self.backView.hidden = NO;
    }else
    {
        self.backView.hidden = YES;
    }
    
}

- (void)rongyunConnect
{
    [[RCIM sharedRCIM] connectWithToken:@"qhprPi6NKHjpUb/Y59pGxyla6KNAky4Sdf6EZcLIXsGTvCxKsa1xnwplowbwo+KpV88SqdKsx3oXWd46fIX1+Q==" success:^(NSString *userId) {
        NSLog(@"登陆成功。当前登录的用户ID：%@", userId);
        
        
        //设置用户信息提供者,页面展现的用户头像及昵称都会从此代理取
        //            [[RCIM sharedRCIM] setUserInfoDataSource:self];
        NSLog(@"Login successfully with userId: %@.", userId);
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //                CustumerVC *custumerVC = [[CustumerVC alloc] init];
            //                [self.navigationController pushViewController:custumerVC animated:YES];
            
            NSLog(@"push");
        });
        
        
    } error:^(RCConnectErrorCode status) {
        NSLog(@"登陆的错误码为:%ld", status);
    } tokenIncorrect:^{
        //token过期或者不正确。
        //如果设置了token有效期并且token过期，请重新请求您的服务器获取新的token
        //如果没有设置token有效期却提示token错误，请检查您客户端和服务器的appkey是否匹配，还有检查您获取token的流程。
        NSLog(@"token错误");
    }];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];// 取消选中

//    if (indexPath.section ==0 && indexPath.row == 1) {
//        
//        MYShipinshouchang_ *xieyite = [[MYShipinshouchang_ alloc]init];
//        [self.navigationController pushViewController:xieyite animated:YES];
//        
//    }
     if (indexPath.section == 0 && indexPath.row == 2) {
    
        [[RCIM sharedRCIM] connectWithToken:@"qhprPi6NKHjpUb/Y59pGxyla6KNAky4Sdf6EZcLIXsGTvCxKsa1xnwplowbwo+KpV88SqdKsx3oXWd46fIX1+Q==" success:^(NSString *userId) {
            NSLog(@"登陆成功。当前登录的用户ID：%@", userId);
            
            
            //设置用户信息提供者,页面展现的用户头像及昵称都会从此代理取
//            [[RCIM sharedRCIM] setUserInfoDataSource:self];
            NSLog(@"Login successfully with userId: %@.", userId);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                CustumerVC *custumerVC = [[CustumerVC alloc] init];
                [self.navigationController pushViewController:custumerVC animated:YES];

                //隐藏tabBar
                self.tabBarController.tabBar.hidden = YES;
                NSLog(@"push");
            });
            
            
        } error:^(RCConnectErrorCode status) {
            NSLog(@"登陆的错误码为:%ld", status);
        } tokenIncorrect:^{
            //token过期或者不正确。
            //如果设置了token有效期并且token过期，请重新请求您的服务器获取新的token
            //如果没有设置token有效期却提示token错误，请检查您客户端和服务器的appkey是否匹配，还有检查您获取token的流程。
            NSLog(@"token错误");
        }];

    if (indexPath.section ==0 && indexPath.row == 1) {
        
        MYShipinshouchang_ *xieyite = [[MYShipinshouchang_ alloc]init];
        [self.navigationController pushViewController:xieyite animated:YES];
        
    }else if (indexPath.section == 0 && indexPath.row == 2) {
        
        
        RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
        conversationVC.conversationType =ConversationType_PRIVATE;
        conversationVC.targetId = @"tour"; //这里模拟自己给自己发消息，您可以替换成其他登录的用户的UserId
        //                conversationVC.userName = @"测试1";
        conversationVC.title = @"客服咨询";
        [self.navigationController pushViewController:conversationVC animated:YES];
        //隐藏tabBar
        self.tabBarController.tabBar.hidden = YES;



    }
   
    
     }
    if (indexPath.section == 2 && indexPath.row ==1) {
        MyhuanfuViewController *m = [[MyhuanfuViewController alloc]init];
        [self.navigationController pushViewController:m animated:YES];
        NSLog(@"asd");
    }
    
}

/**
 *此方法中要提供给融云用户的信息，建议缓存到本地，然后改方法每次从您的缓存返回
 */
- (void)getUserInfoWithUserId:(NSString *)userId completion:(void(^)(RCUserInfo* userInfo))completion
{
    //此处为了演示写了一个用户信息
    if ([@"1" isEqual:userId]) {
        RCUserInfo *user = [[RCUserInfo alloc]init];
        user.userId = @"tour";
        user.portraitUri = @"https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1756054607,4047938258&fm=96&s=94D712D20AA1875519EB37BE0300C008";
        
        return completion(user);
    }else if([@"2" isEqual:userId]) {
        RCUserInfo *user = [[RCUserInfo alloc]init];
        user.userId = @"2";
//        user.name = @"测试2";
        user.portraitUri = @"https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1756054607,4047938258&fm=96&s=94D712D20AA1875519EB37BE0300C008";
        return completion(user);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
